#! /bin/bash


##############################
# Helper Functions
##############################

vercomp () {
    # compare doxygen version number to min version
    # http://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format
    if [[ $1 == $2 ]]
    then
        return 0
    fi
    local IFS=.
    local i ver1=($1) ver2=($2)
    # fill empty fields in ver1 with zeros
    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
    do
        ver1[i]=0
    done
    for ((i=0; i<${#ver1[@]}; i++))
    do
        if [[ -z ${ver2[i]} ]]
        then
            # fill empty fields in ver2 with zeros
            ver2[i]=0
        fi
        if ((10#${ver1[i]} > 10#${ver2[i]}))
        then
            return 1
        fi
        if ((10#${ver1[i]} < 10#${ver2[i]}))
        then
            return 2
        fi
    done
    return 0
}

testvercomp () {
    vercomp $1 $2
    case $? in
        0) op='=';;
        1) op='>';;
        2) op='<';;
    esac
    if [[ $op != $3 ]]
    then
        echo "FATAL ERROR:  Found Doxygen version ($1).
              Must be at least $2 to generate the docs.
              You can find pre-generated docs on our website:
              http://www.socsci.uci.edu/~jkrichma/CARL/CARLsim"
        exit 0
    fi
}


##############################
# Main Script
##############################

if ! command -v doxygen &> /dev/null
then
    echo "ERROR: Could not find the 'doxygen' command on your system."
    echo "       You'll need to install doxygen before building the docs."
    exit
fi

# get doxygen version
doxVer="$(doxygen --version | grep ^[0-9]\.[0-9])"

# require min version
minVer=1.8
testvercomp $doxVer $minVer '>'

doxygen CARLsim.config.doxy
